# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : install.py
# Date      : 2018/8/25
# Version   : 1.0.1
# -------------------------------

import os
import sys
import getpass

try:
    import maya.mel
    import maya.cmds
    isMaya = True
except ImportError:
    isMaya = False

def __modules():
    """

    """
    srcPath = os.path.join(os.path.dirname(__file__))
    srcPath = os.path.normpath(srcPath)
    user_name = getpass.getuser()
    # C:\Users\Admin\Documents\maya\modules
    modules_path = os.path.join("C:\Users",user_name,"Documents\maya\modules")
    if not os.path.exists(modules_path):
        os.makedirs(modules_path)
    modules_file = "lhotkey.mod"
    modules_file_path = os.path.join(modules_path,modules_file)
    if not os.path.exists(modules_file_path):
        command = ("""
+ lhotkey any {scrpts_path}
PYTHONPATH += {scrpts_path}
        """.format(scrpts_path=srcPath))
        with open(modules_file_path,"w") as mf:
            mf.write(command)
    return

def _onMayaDropped():
    """Dragging and dropping this file into the scene executes the file."""
    srcPath = os.path.join(os.path.dirname(__file__))
    srcPath = os.path.normpath(srcPath)

    for path in sys.path:
        if os.path.exists(os.path.join(path , '__init__.py')):
            maya.cmds.warning('lhotkey is already installed at ' + path)

    command = (
'''
# -----------------------------------
# lhotkey
# Email : 951035650@qq.com
# -----------------------------------

import os
import sys
    
if not os.path.exists(r'{path}'):
    raise IOError(r'The source path "{path}" does not exist!')
    
if r'{path}' not in sys.path:
    sys.path.insert(0, r'{path}')
    
import lhotkey
lhotkey.setup()
'''.format(path=srcPath))

    shelf = maya.mel.eval('$gShelfTopLevel=$gShelfTopLevel')
    parent = maya.cmds.tabLayout(shelf, query=True, selectTab=True)
    maya.cmds.shelfButton(
        command=command,
        annotation='lhotkey',
        sourceType='Python',
        parent=parent
    )

def lhotkey_setup():
    srcPath = os.path.join(os.path.dirname(__file__))
    srcPath = os.path.normpath(srcPath)
    if not os.path.exists(srcPath):
        raise IOError(r'The source path "{path}" does not exist!'.format(path=srcPath))
    
    if r'{path}' not in sys.path:
        sys.path.insert(0, srcPath)
    import scripts.userSetup as userSetup
    
    import lhotkey
    reload(lhotkey)
    lhotkey.setup()
    lhotkey.hotkeyCmd("help")
    return

if isMaya:
    __modules()
    # _onMayaDropped()
    lhotkey_setup()
    
if __name__ == '__main__':
    pass